package com.bbzn.device.client.service.impl;

import com.alibaba.fastjson.JSON;
import com.bbzn.device.client.constant.YG;
import com.bbzn.device.client.dto.api.ct.request.RequestBatchCreateDevice;
import com.bbzn.device.client.dto.api.ct.request.RequestCreateDevice;
import com.bbzn.device.client.service.DeviceManagementService;
import com.ctg.ag.sdk.biz.AepDeviceManagementClient;
import com.ctg.ag.sdk.biz.AepNbDeviceManagementClient;
import com.ctg.ag.sdk.biz.aep_device_management.CreateDeviceRequest;
import com.ctg.ag.sdk.biz.aep_device_management.CreateDeviceResponse;
import com.ctg.ag.sdk.biz.aep_device_management.DeleteDeviceRequest;
import com.ctg.ag.sdk.biz.aep_device_management.DeleteDeviceResponse;
import com.ctg.ag.sdk.biz.aep_nb_device_management.BatchCreateNBDeviceRequest;
import com.ctg.ag.sdk.biz.aep_nb_device_management.BatchCreateNBDeviceResponse;
import org.springframework.stereotype.Service;

/**
 * @Author wulongbo
 * @Date 2021/9/9 16:50
 * @Version 1.0
 */
@Service
public class DeviceManagementServiceImpl implements DeviceManagementService {


    @Override
    public CreateDeviceResponse createDevice(String masterKey, RequestCreateDevice requestCreateDevice) throws Exception {
        // 1.bean对象转换成byte数组
        byte[] body = JSON.toJSONString(requestCreateDevice).getBytes();
        AepDeviceManagementClient client = AepDeviceManagementClient.newClient()
                .appKey(YG.APP_KEY)
                .appSecret(YG.APP_SECRET)
                .build();
        CreateDeviceRequest request = new CreateDeviceRequest();
        request.setParamMasterKey(masterKey);    // single value
        request.setBody(body);    //具体格式见前面请求body说明

        CreateDeviceResponse createDeviceResponse = client.CreateDevice(request);
        System.out.println(createDeviceResponse);
        client.shutdown();
        return createDeviceResponse;
    }

    @Override
    public DeleteDeviceResponse deleteDevice(String masterKey, Integer productId, String deviceIds) throws Exception {
        DeleteDeviceRequest request = new DeleteDeviceRequest();
        // set your request params here
        request.setParamMasterKey(masterKey);    // single value
        request.setParamProductId(productId);    // single value
        request.setParamDeviceIds(deviceIds);    // single value
        AepDeviceManagementClient client = AepDeviceManagementClient.newClient()
                .appKey(YG.APP_KEY)
                .appSecret(YG.APP_SECRET)
                .build();
        DeleteDeviceResponse deleteDeviceResponse = client.DeleteDevice(request);
        System.out.println(deleteDeviceResponse);
        return deleteDeviceResponse;
    }

    /**
     * 批量增加NB设备
     *
     * @param requestBatchCreateDevice
     * @return
     * @throws Exception
     */
    @Override
    public BatchCreateNBDeviceResponse batchCreateNBDevice(RequestBatchCreateDevice requestBatchCreateDevice) throws Exception {
        // 1.bean对象转换成byte数组
        byte[] body = JSON.toJSONString(requestBatchCreateDevice).getBytes();
        AepNbDeviceManagementClient client = AepNbDeviceManagementClient.newClient()
                .appKey(YG.APP_KEY)
                .appSecret(YG.APP_SECRET)
                .build();
        BatchCreateNBDeviceRequest request = new BatchCreateNBDeviceRequest();
        // set your request params here
        request.setBody(body);    //具体格式见前面请求body说明
        BatchCreateNBDeviceResponse batchCreateNBDevice = client.BatchCreateNBDevice(request);
        System.out.println(batchCreateNBDevice.toString());
        client.shutdown();
        return batchCreateNBDevice;
    }
}
